﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using pcsse.App_Code.DAL.managementTableAdapters;

namespace pcsse.management
{
    public partial class password : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Priority"] != null)
            {
                int pri = Convert.ToInt32(Session["Priority"].ToString());
                if (pri < 2)
                {
                    Response.Redirect("~/default.aspx");
                }
            }
            errLb.Text = "";
        }

        protected void pDv_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            if (Session["ID"] != null)
            {
                int _id = Convert.ToInt32(Session["ID"]);
                pcsse.App_Code.entity.login l = new pcsse.App_Code.entity.login();
                string opw = ((TextBox)pDv.FindControl("prepasswordTb")).Text;
                int rid = l.LoginByID(_id, opw);
                if (rid == _id)
                {
                    string npw = ((TextBox)pDv.FindControl("newpasswordTb")).Text;
                    string nrepw = ((TextBox)pDv.FindControl("repeatnewTb")).Text;
                    if (npw == "")
                    {
                        errLb.Text = "密码不得为空";
                        e.Cancel = true;
                    }else{
                        if (npw != nrepw)
                        {
                            errLb.Text = "重复输入的密码不匹配";
                            e.Cancel = true;
                        }
                    }
                } 
                else
                {
                    errLb.Text = "原始密码错误";
                    e.Cancel = true;
                }
            }else{
                e.Cancel = true;
            }
        }

        protected void pDvOds_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            if (Session["ID"] == null)
            {
                pDv.ChangeMode(DetailsViewMode.ReadOnly);
            }
            else
            {
                pDv.ChangeMode(DetailsViewMode.Edit);
            }
        }
    }
}
